﻿<%@ WebHandler Language="C#" Class="HandlerUserFavorite" %>

using System;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using Newtonsoft.Json;

public class HandlerUserFavorite : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        string strconn = LinkToDatabase.strConn;/*App_Code->UserClass.cs*/
        string strcmd = "";
        string type = context.Request.Form["type"];
        int user_id = 0;
        if (context.Request.Cookies["u_id"] != null && context.Request.Cookies["u_id"].Value != "null")
        {
            user_id = Convert.ToInt32(context.Request.Cookies["u_id"].Value);//*-***************記得要改Convert.ToInt32(context.Request.Cookies["u_id"].Value)
        }
        using (SqlConnection conn = new SqlConnection(strconn))
        {
            switch (type)
            {
                //1=======================================================================
                case "1":
                    strcmd =
                        //                             SELECT          
                        //                             unboxing_article.unboxing_article_publish_date as date
                        //                             ,unboxing_article.unboxing_article_title as title
                        //                             ,unboxing_article.unboxing_article_content as content
                        //                             FROM   user_favorite_box 
                        //                             INNER JOIN unboxing_article ON user_favorite_box.unboxing_article_id = unboxing_article.unboxing_article_id  
                        //                             where unboxing_article.unboxing_article_id=@uid
                            @"
                            
                             SELECT 
                              CONVERT(varchar(10),unboxing_article.unboxing_article_publish_date,20) as date
                             ,unboxing_article.unboxing_article_title as title
                             , user_favorite_box.unboxing_article_id as aid
                             FROM user_favorite_box join unboxing_article on user_favorite_box.unboxing_article_id=unboxing_article.unboxing_article_id
                             where user_favorite_box.user_id=@Id
                             order by unboxing_article.unboxing_article_publish_date Desc
                             OFFSET @Num ROWS FETCH NEXT @Pagination ROWS ONLY
 
                             ";
                    break;
                //2=======================================================================
                case "2":
                    strcmd = @"select count(*) as num
                                     FROM [UBOX].[dbo].[user_favorite_box]
                                     where [user_id]=@Id
                            ";
                    break;
            }
            using (SqlDataAdapter da = new SqlDataAdapter(strcmd, conn))
            {
                SqlParameter Result;
                Result = new SqlParameter("@Result", SqlDbType.Bit);
                Result.Direction = ParameterDirection.Output;
                switch (type)
                {
                    //1=======================================================================
                    case "1":
                        //input
                        da.SelectCommand.Parameters.AddWithValue("@Id", user_id);
                        da.SelectCommand.Parameters.AddWithValue("@Num", (Convert.ToInt32(context.Request.Form["PageNum"]) - 1) * Convert.ToInt32(context.Request.Form["Pagination"]));
                        da.SelectCommand.Parameters.AddWithValue("@Pagination", Convert.ToInt32(context.Request.Form["Pagination"]));

                        break;

                    //input給參數
                    //da.SelectCommand.Parameters.AddWithValue("@uid", 1);
                    //break;
                    //2=======================================================================
                    case "2":
                        da.SelectCommand.Parameters.AddWithValue("@Id", user_id);
                        break;
                }
                DataSet ds = new DataSet();
                switch (type)
                {
                    //12=======================================================================
                    case "1":
                    case "2":
                        da.Fill(ds, "data");
                        context.Response.ContentType = "application/json";
                        context.Response.Write(JsonConvert.SerializeObject(ds));
                        break;
                }
                conn.Close();
            }
        }        
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}